﻿using DBHelperExample;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Birthday
{
    public partial class Form3 : Form
    {
        private string[] iamge = new string[]
        {
            @"jpg\1.jpg",
            @"jpg\2.jpg",
            @"jpg\3.jpg",
            @"jpg\4.jpg",
            @"jpg\5.jpg",
            @"jpg\6.jpg",
        };
        private static int _currpos;
        public Form3()
        {
            InitializeComponent();
        }
     
        //弹出
        private void button2_Click(object sender, EventArgs e)
        {
                    }
        //新增
        private void button3_Click(object sender, EventArgs e)
        {

            //点击跳转到新增好友信息
            new Thread(() => {
                Application.Run(new Form2());
            }).Start();
            this.Close();
        }
        
        
       /// <summary>
       /// 查询数据
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            //创建ds对象
            DataSet ds = new DataSet();
            //定义查询数据库的语句
            string sql = string.Format("select*from Remind where [Name] like '%{0}%' and [Birthday] like '%{1}%'", textBox1.Text, textBox2.Text);
            //定义DataAdapter对象
            DataTable dap = DBHelper.SelectBvAdapter(sql);
            //设置dataGridView的数据
            dataGridView1.DataSource = dap;
        }
        /// <summary>
        /// 删除好友信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             DialogResult result = MessageBox.Show("您确定要删除此行吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                //取得所选的行号
                int number = dataGridView1.CurrentCell.RowIndex;
                //更新数据库
                string sql = string.Format("delete from Remind where Name='{0}'", dataGridView1.Rows[number].Cells["Column2"].Value.ToString());
                if (DBHelper.GetNonQuery(sql)>0)
                {
                    MessageBox.Show("删除成功");
                   //更新画面表
                   dataGridView1.Rows.RemoveAt(number);
                }
                else
                {
                    MessageBox.Show("请先选择有数据行");
                }
            }
            else
            {
                MessageBox.Show("删除失败");
            }
        }

        /// <summary>
        /// 显示数据库的所有数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form3_Load(object sender, EventArgs e)
        {

            dataGridView1.AutoGenerateColumns = false;
            string sql = string.Format("select*from Remind");
            DataTable dap = DBHelper.SelectBvAdapter(sql);
            dataGridView1.DataSource = dap;
            //让最小化窗口不在任务栏
            this.ShowInTaskbar = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = imageList1.Images[0];
        }

        private void 显示好友照片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var rand = new Random();
            _currpos = rand.Next(0, 6);
            pictureBox1.ImageLocation = iamge[_currpos];
        }

        bool b = false;
        int h, m;

        private void button5_Click(object sender, EventArgs e)
        {
            b = false;
            h = int.Parse(comboBox1.Text);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBox1.Items.Add("一个月内生日");
            this.comboBox1.Items.Add("两个月内生日");
            this.comboBox1.Items.Add("三个月内生日");
        }

        private void 恢复页面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要关闭程序吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要关闭程序吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comboBox1.Text = DateTime.Now.ToString();
            if (b == false)
            {
                if (h == DateTime.Now.Hour)
                {
                    if (m == DateTime.Now.Minute)
                    {
                        b = true;
                        MessageBox.Show("有好友生日快到了!");
                    }
                }
            }
            if (button5.Text == "启动")
            {
                button5.Text = "停止";
                this.timer1.Start();
            }
            else if (button5.Text == "停止")
            {
                button5.Text = "启动";
                this.timer1.Stop();
            }
        }
    }
}


